--------------------------------  Camera system --------------------------------
local IsCameraPlaced = false
CameraEntity = ents.Create("prop_physics")

function EzekRemoveCameraEntity()
	CameraEntity:Remove()
end

function EzekShowCameraEntity()
	if !CameraEntity:IsValid( ) then
		CameraEntity = ents.Create("prop_physics")
	end
	CameraEntity:SetModel( "models/dav0r/camera.mdl" )
	CameraEntity:SetPos( Vector(GetConVarNumber("wots_cm_x"),GetConVarNumber("wots_cm_y"),GetConVarNumber("wots_cm_z")) )
	CameraEntity:SetAngles( LocalPlayer():EyeAngles() )
	CameraEntity:SetOwner( minge )
	CameraEntity:DrawModel()
	CameraEntity:Spawn()
	CameraEntity:SetColor(0, 255, 255, 255)
	CameraEntity:SetMaterial("hlmv/debugmrmwireframe")
end

function EzekRenderSpyCamera()
	local CamData = {}
	CamData.angles = LocalPlayer():EyeAngles()
	CamData.origin = Vector(GetConVarNumber("wots_cm_x"),GetConVarNumber("wots_cm_y"),GetConVarNumber("wots_cm_z"))
	CamData.x = 0
	CamData.y = 0
	CamData.w = ScrW() * GetConVarNumber("wots_cm_w")/100
	CamData.h = ScrH() * GetConVarNumber("wots_cm_h")/100
	render.RenderView( CamData )
	EzekShowCameraEntity()
end

concommand.Add("wots_camerainmypos", function()
	RunConsoleCommand("wots_cm_x", minge:GetPos().x)
	RunConsoleCommand("wots_cm_y", minge:GetPos().y)
	RunConsoleCommand("wots_cm_z", minge:GetPos().z + 60)
end)


concommand.Add("wots_camera", function()
	if IsCameraPlaced then
		EzekRemoveCameraEntity()
		SlobNotify("Toggle camera: OFF")
		IsCameraPlaced = false
		hook.Remove("HUDPaint", "OriginCam")
	else
		SlobNotify("Toggle camera: ON")
		IsCameraPlaced = true
		hook.Add("HUDPaint", "OriginCam", EzekRenderSpyCamera) 
	end
end)

concommand.Add("+wots_movecamera", function(ply, com, args )
	if !CameraEntity:IsValid( ) then
		return false
	else
		timer.Create("cameramove", 0, 9999999999999999, RunConsoleCommand, "+wots_movecamera",args[1])  
		CameraEntity:SetPos( CameraEntity:GetForward()*args[1] + CameraEntity:GetPos() )
		RunConsoleCommand("wots_cm_x", CameraEntity:GetPos().x)
		RunConsoleCommand("wots_cm_y", CameraEntity:GetPos().y)
		RunConsoleCommand("wots_cm_z", CameraEntity:GetPos().z)
	end
end)

concommand.Add("-wots_movecamera", function(ply, com, args )
	timer.Destroy("cameramove")
end)

concommand.Add("wots_camera_follow", function(ply, com, args)
	if CamIsFollowing then
		timer.Destroy("following")
		SlobNotify("Stop following")
		CamIsFollowing = false
		return false
	end		
	local PlayerExist = false
	for _,v in pairs(player.GetAll()) do
		if GetConVarString("wots_followingto") == v:Nick() then
			PlayerExist = true
		end
	end
	if !PlayerExist then 
		SlobNotify("Player doesn't exist")
		return false 
	end
	SlobNotify("Start following")
	CamIsFollowing = true
	timer.Create("following",0,9999999999999999, function()
		for _,v in pairs(player.GetAll()) do
			if v:Nick() == GetConVarString("wots_followingto") then
				RunConsoleCommand("wots_cm_x", v:GetPos().x)
				RunConsoleCommand("wots_cm_y", v:GetPos().y)
				RunConsoleCommand("wots_cm_z", v:GetPos().z+55)
			end
		end
	end)
end)
------------------------------------------------------------------------------